<?php

/*
 * 用户操作类
 */

namespace Home\Logic;

class StoreLogic {

    public function __construct() {
        $this->s = D('Store');
        import('Memcachectr');
        $this->m = \Think\Memcachectr::create();
    }

    public function insertStore($arr) {
        $arr['create_time'] = time();
        $this->s->startTrans();
        $id = $this->s->add($arr);
        if ($id < 1) {
            $this->s->rollback();
            return false;
        }
        $c = D('Companystoremap', 'Logic');
        $com = $c->getCompanyMap($arr['user_id'], true);
        if (strlen($com[0]['company_number']) > 1) {
            $company_number = $com[0]['company_number'];
        } else {
            $company_number = $c->createNumber();
        }
        $admin_id = $arr['user_id'];
        $store_admin_id = $arr['user_id'];
        $re = $c->insertCompanyMap($admin_id, $store_admin_id, $id, $company_number);
        if (!$re) {
            $this->s->rollback();
            return false;
        }
        $this->s->commit();
        return $id;
    }

    public function updateStore($data) {
        $b = D('Barber', 'Logic');
        if ($data['barber_id'] > 0) {
            $this->s->startTrans();
            $barber = $b->getBarber($data['barber_id']);
            $p = D('Passport', 'Logic');
            $_user = $p->getUserByAccount($barber['mobile']);
            if ($barber['user_id'] < 1) {
                if ($_user['id'] > 0) {
                    $user_id = $_user['id'];
                }else{
                    //$password = array_rand(range(0, 9), 6);
                    $password = '';
                    for($i=0;$i<6;$i++){
                        $password .= rand(0,9);
                    }
                    $user_id = $p->addUser($barber['name'], $barber['mobile'], $password, 2);
                     //发任命店长短信
                    if($user_id){
                        $store = $this->getStore($data['id']);
                        import('MsgCode','Msgcode');
                        $m = new \Msgcode\MsgCode(true);
                        //$password = implode('',$password);
                        $_contents = '7&1&'.$store['name'].'&'.$barber['mobile'].'&'.$password;
                        $res = $m->sendMsg_b($barber['mobile'],$_contents,true);
                    }
                    
                }
                if (!$user_id) {
                    $this->s->rollback();
                    return false;
                }
                $data['user_id'] = $user_id;
            }else {
                $data['user_id'] = $barber['user_id'] > 0 ? $barber['user_id'] : (int) $_user['id'];
            }
        }
       


        $re = $this->s->where(array('id' => $data['id']))->save($data);
        if ($re === false && $data['barber_id'] > 0) {
            $this->s->rollback();
            return false;
        }
        if ($data['user_id'] > 0 && $barber['id'] > 0) {
            $re = $b->updateBarber(array('id' => $data['barber_id'], 'user_id' => $data['user_id']));
            if ($re === false) {
                $this->s->rollback();
                return false;
            }
        }
        if ($data['barber_id'] > 0) {
            $this->s->commit();
        }
        $this->m->delete('barber_store_' . $barber['store_id']);
        $this->m->delete('store_' . $data['id']);
        return $re;
    }

    public function getStore($id) {
        $re = $this->m->get('store_' . $id);
        if (!$re) {
            $re = $this->s->where(array('id' => $id))->find();
            $this->m->set('store_' . $id, $re, true);
        }
        return $re;
    }

    public function getStoreByUserId() {
        return $this->s->where(array('user_id' => $_SESSION['id']))->find();
    }

    public function getStores() {
        $re = $this->s->where(array('user_id' => $_SESSION['id']))->select();
        return $re;
    }

    public function getStoresByIds($cid) {
        if ($cid == NULL || count($cid) < 1) {
            return;
        }
        $cid = implode(',', (array) $cid);
        return $this->s->where('id in (' . $cid . ')')->select();
    }

    public function getBlocByStoreId($id) {
        $csm = new \Home\Logic\CompanystoremapLogic();
        $store = $csm->getByStoreId($id);
        $stores = $csm->getStoreByNumber($store['company_number']);
        $s = array();
        foreach ($stores as $v) {
            $store = $this->getStore($v['store_id']);
            $s[$v['store_id']] = $store['name'];
        }
        return $s;
    }
    
    /*按名称模糊匹配门店*/
    public function searchStore($name=false,$page,$limit=20){
        $total_page = ceil($this->countStore($name)/$limit);
        $total_page = $total_page>0?$total_page:1;
        $page = $page>0?$page:1;
        $page = $page>$total_page?$total_page:$page;
        if($name != false){
            $where = ' where name like "%'.$name.'%"';
        }
        $sql = 'select * from store'.$where.' order by id desc limit '.(($page-1)*$limit).','.$limit;
        $re = $this->s->_read_data($this->s->query($sql));
        $_Page  = new \Think\Page($total_page*$limit,$limit);
        $show  = $_Page->show();
        $show = str_replace('/index.php/','/',$show);
        return array('page'=>$show,'list'=>$re);
    }
    
    public function countStore($name=false){
        if($name != false){
            $where = ' where name like "%'.$name.'%"';
        }
        $sql = 'select count(*) as c from store'.$where;
        $re =  $this->s->_read_data($this->s->query($sql));
        return $re[0]['c'];
    }
    
    
    /*只更新店面状态*/
    public function updateStoreStatus($id,$status){
        return $this->s->where('id='.$id)->save(array('status'=>$status));
    }

}
